package com.bawei.mall.pay.controller;

import com.bawei.mall.pay.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.pay.controller
 * @Project：mall_cloud
 * @name：PayController
 * @Date：2024/7/11 14:07
 */
@RestController
@RequestMapping("/pay")
public class PayController {
    @Resource
    private IPayService aliPayServiceImpl;
    @Resource
    private IPayService weChatServiceImpl;


    @GetMapping("/webPay")
    public void webPay(HttpServletResponse response, @RequestParam Long orderId,@RequestParam Integer payType){
        String formHtml = null;

        switch (payType){
            case 1:
                formHtml = aliPayServiceImpl.webPay(orderId,payType);
            case 2:
                formHtml = weChatServiceImpl.webPay(orderId,payType);
                break;
            default:
                throw new RuntimeException("支付类型错误");
        }

        response.setContentType("text/html;charset=UTF-8");
        if(formHtml == null){
           throw new RuntimeException("支付类型错误,请联系管理员");
        }
        try {
            response.getWriter().write(formHtml);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
